package com.chushouya.controller.common;

import com.chushouya.common.constant.ServiceType;
import com.chushouya.common.constant.DangDangConstants;
import com.chushouya.order.dto.api.dangdang.DangDangCreateOrderRequest;
import com.chushouya.order.dto.api.dangdang.DangDangCreateOrderResponse;
import com.chushouya.order.dto.api.dangdang.DangDangResponse;
import com.chushouya.order.service.api.DangDangApiService;
import com.chushouya.common.util.DangDangSignUtil;
import com.general.framework.core.schema.Response;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 铛铛对接测试控制器
 */
@Api(tags = ServiceType.COMMON + "铛铛对接测试")
@RestController
@RequestMapping(ServiceType.COMMON + "/dangdang")
@Slf4j
public class DangDangTestController {

    @Resource
    private DangDangApiService dangDangApiService;

    @ApiOperation("测试签名算法")
    @GetMapping("/testSign")
    public Response<String> testSign() {
        String timestamp = DangDangSignUtil.getCurrentTimestamp();
        String sign = DangDangSignUtil.generateSign(
                DangDangConstants.APP_ID, 
                DangDangConstants.APP_SECRET, 
                timestamp
        );
        
        String result = String.format("时间戳: %s, 签名: %s", timestamp, sign);
        log.info("签名测试结果: {}", result);
        
        return Response.success(result);
    }

    @ApiOperation("创建测试订单")
    @PostMapping("/createTestOrder")
    public Response<DangDangResponse<DangDangCreateOrderResponse>> createTestOrder() {
        // 创建测试订单数据
        DangDangCreateOrderRequest request = new DangDangCreateOrderRequest();
        request.setAddress("北京 北京市 昌平区 测试地址");
        request.setChannelId("1007");
        
        // 设置时间为明天
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
        SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
        Date tomorrow = new Date(System.currentTimeMillis() + 24 * 60 * 60 * 1000);
        Date startDateTime = new Date(tomorrow.getTime() + 9 * 60 * 60 * 1000); // 明天上午9点
        Date endDateTime = new Date(tomorrow.getTime() + 11 * 60 * 60 * 1000);   // 明天上午11点
        
        String startTime = sdf.format(startDateTime);
        String endTime = sdf.format(endDateTime);
        String startTimeOnly = timeFormat.format(startDateTime);
        String endTimeOnly = timeFormat.format(endDateTime);
        
        request.setStarttime(startTime);
        request.setEndtime(endTime);
        request.setGoGomeTime(sdf.format(startDateTime).substring(0, 10) + " " + startTimeOnly + "-" + endTimeOnly);
        
        request.setTel("13800138000");
        request.setType(DangDangConstants.ORDER_TYPE_CLOTHES);
        request.setUserName("测试用户");
        request.setWeight("65-100kg");
        request.setOrderSn("CS_TEST_" + System.currentTimeMillis());
        request.setOpenId("test_user_" + System.currentTimeMillis());
        
        log.info("创建铛铛测试订单: {}", request.getOrderSn());
        
        // 调用测试环境API
        DangDangResponse<DangDangCreateOrderResponse> result = dangDangApiService.createOrderTest(request);
        
        return Response.success(result);
    }

    @ApiOperation("创建正式订单")
    @PostMapping("/createOrder")
    public Response<DangDangResponse<DangDangCreateOrderResponse>> createOrder(
            @RequestBody DangDangCreateOrderRequest request) {
        
        log.info("创建铛铛正式订单: {}", request.getOrderSn());
        
        // 调用正式环境API
        DangDangResponse<DangDangCreateOrderResponse> result = dangDangApiService.createOrder(request);
        
        return Response.success(result);
    }
}
